package com.wbdgj.ui.mine;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.igexin.sdk.PushManager;
import com.wbdgj.R;
import com.wbdgj.base.BaseActivity;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpVersionAdapter;
import com.wbdgj.http.OnNoResponseListener;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultRefeshModel;
import com.wbdgj.model.ResultVersionModel;
import com.wbdgj.ui.UpdateDialogActivity;
import com.wbdgj.ui.home.NewsXtxxActivity;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.ui.login.RegisterActivity;
import com.wbdgj.utils.LogUtils;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.dialog.ConfirmDialog;
import com.wbdgj.utils.dialog.ConfirmVersionDialog;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * Created by Administrator on 2018/6/1.
 */

public class SettingActivity extends BaseActivity{
    private Context context = this;
    @BindView(R.id.version)
    TextView version;
    private PackageInfo packageInfo;
    @Override
    protected int getLayoutResId() {
        return R.layout.ui_setting;
    }

    @Override
    protected void initView() {
        PackageManager packageManager = context.getPackageManager();
        try {
            packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        version.setText("版本："+packageInfo.versionName+"（更新时间：2020-01-04）");
    }

    @OnClick(value = {R.id.bbjsLay, R.id.quitLay, R.id.changepassword, R.id.zhaqLay, R.id.aboutLay, R.id.jcgxLay})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.jcgxLay:
                isUpdate();
                break;
            case R.id.aboutLay:
                launch(AboutActivity.class);
                break;
            case R.id.zhaqLay:
                launch(InformaticaNewActivity.class);
                break;
            case R.id.bbjsLay:
                launch(NewsXtxxActivity.class);
                break;
            case R.id.quitLay:
                final ConfirmDialog confirmDialog = new ConfirmDialog(context, "确定退出吗？", 1);
                confirmDialog.show();
                confirmDialog.setCanceledOnTouchOutside(false);
                confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
                    @Override
                    public void doConfirm() {
                        confirmDialog.dismiss();
                        BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                        BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                        PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                        Intent intent = new Intent(context, LoginActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    }

                    @Override
                    public void doCancel() {
                        confirmDialog.dismiss();
                    }
                });
                break;
            case R.id.changepassword:
                launch(ChangePasswordActivity.class);
                break;

            default:
                break;
        }
    }
    private String versionCode;
    /**
     * 判断是否有更新
     */
    private void isUpdate() {

        versionCode = packageInfo.versionName;
        Call<ResultRefeshModel<ResultVersionModel>> call = HttpVersionAdapter.getSerives().version();
        call.enqueue(new OnResponseListener<ResultRefeshModel<ResultVersionModel>>(context) {
            @Override
            protected void onSuccess(final ResultRefeshModel<ResultVersionModel> followModelBaseResultModel) {
                if (followModelBaseResultModel.getCode() == 0){
                    double version01 = Double.parseDouble(versionCode);
                    double version02 = Double.parseDouble(followModelBaseResultModel.getData().getBuildVersion());
                    if(version02>version01){
                        startActivity(new Intent(context, UpdateDialogActivity.class).putExtra("content", followModelBaseResultModel.getData().getBuildUpdateDescription()).putExtra("url", followModelBaseResultModel.getData().getDownloadURL()));
                    }else{
                        final ConfirmVersionDialog confirmVersionDialog = new ConfirmVersionDialog(context, "当前是最新版本 V "+packageInfo.versionName, 1);
                        confirmVersionDialog.show();
                        confirmVersionDialog.setCanceledOnTouchOutside(false);
                        confirmVersionDialog.setClicklistener(new ConfirmVersionDialog.ClickListenerInterface() {
                            @Override
                            public void doConfirm() {
                                confirmVersionDialog.dismiss();

                            }

                            @Override
                            public void doCancel() {
                                confirmVersionDialog.dismiss();
                            }
                        });
                    }
                }


            }

            @Override
            protected void onFailure(String msg) {
                super.onFailure(msg);
            }
        });

    }
}
